package com.unissoft.ordersystem.dataaccess.common;

import java.util.ArrayList;
import java.util.List;

/**
 * BaseCriteria: 封装数据库查询条件的基类
 * @author aaron
 * 
 */
public abstract class BaseCriteria {
    private String orderByClause;

    private SortMark sortMark;

    private boolean distinct;

    private List<Criterion> criterias = new ArrayList<Criterion>();

    public abstract void addCriterionEqual(String key, Object value);

    public abstract void addCriterionLike(String key, Object value);

    public abstract void addCriterionIn(String key, List<?> value);

    public abstract void addCriterionBetween(String key, Object value1,
            Object value2);
    
    public abstract void addCriterionGE(String key, Object value);
    
    public abstract void addCriterionLE(String key, Object value);
    
    public abstract void addCriterionNotEqual(String key, Object value);

    /**
     * 增加查询条件
     * 
     * @param conditionColumn
     *            sql中的条件
     * @param value
     *            sql需要被比较的值
     */
    public void addCriterion(String conditionColumn, Object value) {
        this.addCriterion(conditionColumn, null, value);
    }

    public void addCriterion(String conditionColumn, String conditionOperate,
            Object value, String property) {
        if (value == null) {
            throw new RuntimeException(property
                    + "todo");
        }
        this.addCriterion(conditionColumn, conditionOperate, value);
    }
    
    public void addCriterion(String conditionColumn, String operate,
            Object value1, Object value2, String property) {
        if (value1 == null || value2 == null) {
            throw new RuntimeException(property
                    + "todo");
        }
        this.addCriterion(conditionColumn, operate, value1, value2);
    }

    private void addCriterion(String conditionColumn, String conditionOperate,
            Object value) {
        if (conditionColumn == null) {
            throw new RuntimeException(
                    "todo");
        }
        if (conditionOperate == null) {
            conditionOperate = ConditionOperate.EQUAL;
        }
        criterias.add(new Criterion(conditionColumn, conditionOperate, value));
    }

    private void addCriterion(String conditionColumn, String operate,
            Object value1, Object value2) {
        if (conditionColumn == null) {
            throw new RuntimeException(
                    "todo");
        }
        criterias.add(new Criterion(conditionColumn, operate, value1, value2));
    }

    public void clear() {
        criterias.clear();
        orderByClause = null;
        distinct = false;
    }

    public boolean isValid() {
        return criterias.size() > 0;
    }

    // getter and setter
    public String getOrderByClause() {
        return orderByClause;
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public SortMark getSortMark() {
        return sortMark;
    }

    public void setSortMark(SortMark sortMark) {
        this.sortMark = sortMark;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public List<Criterion> getCriterias() {
        return criterias;
    }

    public void setCriterias(List<Criterion> criterias) {
        this.criterias = criterias;
    }

    public static class ConditionOperate {
        public static final String EQUAL = "=";
        public static final String LIKE = "LIKE";
        public static final String IN = "IN";
        public static final String BETWEEN = "BETWEEN";
        public static final String IS = "IS";
        public static final String ISNOT = "IS NOT";
        public static final String GE = ">=";
        public static final String LE = "<=";
        public static final String NOT_EQUAL = "!=";
    }
    
}
